package com.example.sixtrainingdemo.view;

import android.annotation.SuppressLint;
import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.example.sixtrainingdemo.R;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout mTabOne;
    private LinearLayout mTabTwo;
    private LinearLayout mTabMessage;
    private LinearLayout mTabMine;
    private FirstFragment mFirstFragment;
    private MineFragment mMineFragment;
    private MessageFragment mMessageFragment;
    private VideoFragment mVideoFragment;
    private Fragment mContentFragment;
    private FragmentManager mFragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTabOne = findViewById(R.id.ll_tab_one);
        mTabTwo = findViewById(R.id.ll_tab_two);
        mTabMessage = findViewById(R.id.ll_tab_message);
        mTabMine = findViewById(R.id.ll_tab_mine);

        mTabOne.setOnClickListener(this);
        mTabTwo.setOnClickListener(this);
        mTabMessage.setOnClickListener(this);
        mTabMine.setOnClickListener(this);
        initFragment();
        changeFragment(mFirstFragment);
        mFragmentManager.beginTransaction()
                .add(R.id.fragment_content, mFirstFragment).commitAllowingStateLoss();
        mContentFragment = mFirstFragment;
        mTabOne.setSelected(true);

    }

    private void initFragment() {
        mFragmentManager = getSupportFragmentManager();
        mFirstFragment = new FirstFragment();
        mMineFragment = new MineFragment();
        mMessageFragment = new MessageFragment();
        mVideoFragment = new VideoFragment();
    }

    public void changeFragment(Fragment to) {
        if (mContentFragment != null && mContentFragment != to) {
            if (!to.isAdded()) {
                mFragmentManager.beginTransaction().hide(mContentFragment)
                        .add(R.id.fragment_content, to).commitAllowingStateLoss();
            } else {
                mFragmentManager.beginTransaction().hide(mContentFragment)
                        .show(to).commitAllowingStateLoss();
            }
            mContentFragment = to;
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ll_tab_one) {
            mTabOne.setSelected(true);
            mTabTwo.setSelected(false);
            mTabMessage.setSelected(false);
            mTabMine.setSelected(false);
            changeFragment(mFirstFragment);
        } else if (id == R.id.ll_tab_two) {
            mTabOne.setSelected(false);
            mTabTwo.setSelected(true);
            mTabMessage.setSelected(false);
            mTabMine.setSelected(false);
            changeFragment(mVideoFragment);
        } else if (id == R.id.ll_tab_message) {
            mTabOne.setSelected(false);
            mTabTwo.setSelected(false);
            mTabMessage.setSelected(true);
            mTabMine.setSelected(false);
            changeFragment(mMessageFragment);
        } else if (id == R.id.ll_tab_mine) {
            mTabOne.setSelected(false);
            mTabTwo.setSelected(false);
            mTabMessage.setSelected(false);
            mTabMine.setSelected(true);
            changeFragment(mMineFragment);
        }
    }
}